﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Modeling;
using BizAppFactory.Domain;

namespace BizAppFactory.DomainModel.CustomEditors
{
    public class EditorHelper
    {
        public static Store GetStore(object gridSelection)
		{
			// We assume that "instance" will either be a single model element, or 
			// an array of model elements (if multiple items are selected).

			ModelElement currentElement = null;

			object[] objects = gridSelection as object[];
			if (objects != null && objects.Length > 0)
			{
				currentElement = objects[0] as ModelElement;
			}
			else
			{
				currentElement = gridSelection as ModelElement;
			}

			return (currentElement == null) ? null : currentElement.Store;
		}

        public static BizAppDomainModel GetModel(object gridSelection)
        {
            Store store = GetStore(gridSelection);
            foreach (ModelElement modelElement in store.DefaultPartition.ElementDirectory.AllElements)
            {
                if (modelElement.GetType() ==  typeof(BizAppDomainModel))
                {
                    return (BizAppDomainModel)modelElement;
                }
            }

            throw new ApplicationException("Object of type BizAppDomainModel cannot e found in store.DefaultPartition.ElementDirectory");

        }
    }
}
